package com.bird.weichat.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bird.weichat.common.BaseResponseVO;
import com.bird.weichat.conf.WeiChatTemplateProperties;
import com.bird.weichat.server.WeiChatTemplateServer;
import com.bird.weichat.utils.FileUtils;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author sijunqiang
 * @data 2021/7/19 23:15
 * @description: 微信小程序 控制层
 */
@RequestMapping(value = "/v1")
@RestController
public class WeiChatTemplateController {

    @Autowired
    private WeiChatTemplateProperties properties;


    @Autowired
    private WeiChatTemplateServer server;

    // 获取模板内容
    @RequestMapping(value = "/template", method = RequestMethod.GET)
    public BaseResponseVO getTemplate() {
        WeiChatTemplateProperties.WeiChatTemplate weiChatTemplate = server.getWeiChatTemplate();
        Map<String, Object> result = Maps.newHashMap();
        result.put("templateId", weiChatTemplate.getTemplateId());
        result.put("template", FileUtils.readFile2JsonArray(weiChatTemplate.getTemplateFilePath()));
        return BaseResponseVO.success(result);
    }

    //获取调查结果
    @RequestMapping(value = "/template/result", method = RequestMethod.GET)
    public BaseResponseVO templateStatistics(@RequestParam(value = "templateId", required = false) String templateId) {
        JSONObject jsonObject = server.templateStatistics(templateId);
        return  BaseResponseVO.success(jsonObject);
    }

    // 上传调研内容
    @RequestMapping(value = "/template/report", method = RequestMethod.POST)
    public BaseResponseVO dataReported(@RequestBody String reportData) {
        server.templateReported(JSON.parseObject(reportData));
        return BaseResponseVO.success();
    }


}
